﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;

namespace 基于WPF的电缆采样数据分析系统.ViewModels
{
    public class MainWinViewModel : BindableBase
    {


        #region 左侧菜单

        /// <summary>
        /// 左侧菜单
        /// </summary>
        private List<LeftMenuInfo> _LeftMenuList;

        /// <summary>
        /// 左侧菜单
        /// </summary>
		public List<LeftMenuInfo> LeftMenuList
        {
            get { return _LeftMenuList; }
            set
            {
                _LeftMenuList = value;
                RaisePropertyChanged();
            }
        }



        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public MainWinViewModel(IRegionManager _RegionManager)
        {
            // 创建菜单数据
            LeftMenuList = new List<LeftMenuInfo>();
            CreateMenu();

            // 区域管理器配置
            RegionManager = _RegionManager;

            // 导航切换方法
            NavigateCmm = new DelegateCommand<LeftMenuInfo>(NavigateMenu);

            // 前进后退
            GoBackCmm = new DelegateCommand(GoBack);
            GoForwardCmm = new DelegateCommand(GoForward);

            // 回到首页
            GoHomeCmm = new DelegateCommand<string>(GoHome);
        }


        #region 默认状态

        /// <summary>
        /// 创建菜单数据
        /// </summary>
        private void CreateMenu()
        {
            LeftMenuList.Add(new LeftMenuInfo() { Icon = "Waveform", MenuName = LangugeConvertor.GetValue("TDRWorkSpace"), ViewName = "TDRWorkUC" });
            LeftMenuList.Add(new LeftMenuInfo() { Icon = "Devices", MenuName = LangugeConvertor.GetValue("DeviceStatusArea"), ViewName = "DeviceStatusUC" });
            LeftMenuList.Add(new LeftMenuInfo() { Icon = "Grid", MenuName = LangugeConvertor.GetValue("ReportAnalysisArea"), ViewName = "AccountingReportUC" });
            LeftMenuList.Add(new LeftMenuInfo() { Icon = "History", MenuName = LangugeConvertor.GetValue("HistoricalDataArea"), ViewName = "HistoricalDataUC" });
            LeftMenuList.Add(new LeftMenuInfo() { Icon = "Cog", MenuName = LangugeConvertor.GetValue("SystemSettingsArea"), ViewName = "SettingsUC" });

        }

        /// <summary>
        ///  默认首页
        /// </summary>
        public void SetDefaultNav(string loginName)
        {
            NavigationParameters paras = new NavigationParameters();
            paras.Add("LoginName", loginName);
            RegionManager.Regions["MainViewRegion"].RequestNavigate("TDRWorkUC",
                callback =>
                {
                    Journal = callback.Context.NavigationService.Journal; // 记录足迹
                },
                paras
            );
        }

        #endregion


        #region 导航

        /// <summary>
        /// 区域管理器
        /// </summary>
        private readonly IRegionManager RegionManager;

        /// <summary>
        /// 导航命令
        /// </summary>
        public DelegateCommand<LeftMenuInfo> NavigateCmm { get; set; }

        /// <summary>
        /// 左侧菜单导航切换
        /// </summary>
        /// <param name="menu">对应的菜单信息</param>
        private void NavigateMenu(LeftMenuInfo menu)
        {
            if (menu == null || string.IsNullOrEmpty(menu.ViewName))
            {
                return;
            }
            RegionManager.Regions["MainViewRegion"].RequestNavigate(menu.ViewName,
                callback =>
                {
                    Journal = callback.Context.NavigationService.Journal; // 记录导航足迹
                });
        }

        #endregion


        #region 前进 后退
        /// <summary>
        /// 历史记录
        /// </summary>
        private IRegionNavigationJournal Journal;

        /// <summary>
        /// 后退命令
        /// </summary>
        public DelegateCommand GoBackCmm { get; private set; }

        /// <summary>
        /// 前进命令
        /// </summary>
        public DelegateCommand GoForwardCmm { get; private set; }

        /// <summary>
        /// 后退
        /// </summary>
        private void GoBack()
        {
            if (Journal != null && Journal.CanGoBack)
            {
                Journal.GoBack();
            }
        }

        /// <summary>
        /// 前进
        /// </summary>
        private void GoForward()
        {
            if (Journal != null && Journal.CanGoForward)
            {
                Journal.GoForward();
            }
        }


        #endregion


        #region 回到首页


        /// <summary>
        /// 回到首页命令
        /// </summary>
        public DelegateCommand<string> GoHomeCmm { get; set; }


        /// <summary>
        /// 回到首页
        /// </summary>
        private void GoHome(string homeName)
        {
            RegionManager.Regions["MainViewRegion"].RequestNavigate(homeName, callback =>
            {
                Journal = callback.Context.NavigationService.Journal;
            });
        }

        #endregion


        #region 消息提示框
        /// <summary>
        /// 消息提示
        /// </summary>
        private SnackbarMessageQueue _MessageQueue = new SnackbarMessageQueue();

        /// <summary>
        /// 消息提示
        /// </summary>
        public SnackbarMessageQueue MessageQueue
        {
            get { return _MessageQueue; }
            set
            {
                _MessageQueue = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 显示消息提示框
        /// </summary>
        /// <param name="info"></param>
        public void ShowMessagePromptBox(string info)
        {
            if (MessageQueue is { } messageQueue)
            {
                //use the message queue to send a message.
                var message = info;
                //the message queue can be called from any thread
                Task.Factory.StartNew(() => messageQueue.Enqueue(message));
            }
        }
        #endregion

    }
}
